package com.yuyou.fn.educational.service.impl;

import com.github.wens.framework.service.impl.SuperServiceImpl;
import com.github.wens.mybatisplus.examples.Example;
import com.github.wens.mybatisplus.plugins.Page;
import com.github.wens.mybatisplus.toolkit.IdWorker;
import com.yuyou.fn.common.exception.BusException;
import com.yuyou.fn.common.util.CriteriaUtils;
import com.yuyou.fn.common.util.TimeUtils;
import com.yuyou.fn.common.util.Utils;
import com.yuyou.fn.educational.entity.*;
import com.yuyou.fn.educational.mapper.QuestionnaireSettingMapper;
import com.yuyou.fn.educational.service.*;
import com.yuyou.fn.educational.vo.NaireSettingQueryVo;
import com.yuyou.fn.educational.vo.NaireSettingVo;
import com.yuyou.fn.platform.entity.AreaTeam;
import com.yuyou.fn.platform.service.IAreaTeamService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * QuestionnaireSetting 表数据服务层接口实现类
 *
 */
@Service
public class QuestionnaireSettingServiceImpl extends SuperServiceImpl<QuestionnaireSettingMapper, QuestionnaireSetting,Long> implements IQuestionnaireSettingService {

    @Resource
    private IBaseGradeService baseGradeService;

    @Resource
    private IBaseSubjectService baseSubjectService;

    @Resource
    private ICampusService campusService;

    @Resource
    private IAreaTeamService areaTeamService;

    @Resource
    private IPeriodService periodService;

    @Resource
    private IQuestionnaireService questionnaireService;

    @Override
    public Page<NaireSettingVo> findPageByQueryVo(NaireSettingQueryVo naireSettingQueryVo, int pageNo, int pageSize) {
        Integer start=0;
        if (pageNo==0){
            start=0;
        }else {
            start=(pageNo-1)*pageSize;
        }
        Page<NaireSettingVo> page=new Page<>(pageNo,pageSize);

        Integer total=autoMapper.countByQueryVo(naireSettingQueryVo);
        List<NaireSettingVo> naireSettingVoList=autoMapper.findListByQueryVo(naireSettingQueryVo,start,pageSize);
        if (total==null){
            page.setTotal(0);
        }else {
            page.setTotal(total);
        }
        page.setRecords(naireSettingVoList);
        return page;
    }

    @Override
    public NaireSettingVo findOneBySettingId(Long settingId) {
        if (settingId==null){
            throw new NullPointerException();
        }
        NaireSettingVo naireSettingVo=autoMapper.findOneBySettingId(settingId);
        return naireSettingVo;
    }


    @Override
    public void setUpQuestionnaire(Long settingId, Long questionnaireId) {
        if (settingId==null || questionnaireId==null){
            throw new NullPointerException();
        }
        Questionnaire questionnaire=questionnaireService.findById(questionnaireId,"questionnaireId","questionnaireName");
        Example<QuestionnaireSetting> example=QuestionnaireSetting.createExample();
        example.createCriteria().andEqualTo("settingId",settingId);
        QuestionnaireSetting questionnaireSetting=new QuestionnaireSetting();
        questionnaireSetting.setUseQuestionnaireName(questionnaire.getQuestionnaireName());
        questionnaireSetting.setUseQuestionnaireId(questionnaire.getQuestionnaireId());
        this.updateSelectiveByExample(questionnaireSetting,example);
    }

    @Override
    public void saveOrUpdate(NaireSettingVo naireSettingVo) {
        AreaTeam areaTeam=areaTeamService.findById(naireSettingVo.getAreaTeamId(),"areaTeamId","name");
        Period period=periodService.findById(naireSettingVo.getPeriodId(),"periodId","periodName");
        List<Long> gradeIds= Utils.toLongIds(naireSettingVo.getGradeIds());
        if (gradeIds.size()==0){
            throw new BusException("年级必选");
        }
        List<Long> subjectIds=Utils.toLongIds(naireSettingVo.getSubjectIds());
        if (subjectIds.size()==0){
            throw new BusException("科目必选");
        }

        List<Long> campusIds=Utils.toLongIds(naireSettingVo.getCampusIds());
        if (campusIds.size()==0){
            throw new BusException("校区必选");
        }
        /*setting存在为更新，删除原有设置*/
        Long settingId=null;
        if (naireSettingVo.getSettingId()==null){
            settingId=IdWorker.getId();
        }else {
            settingId=naireSettingVo.getSettingId();
            this.deleteBySettingId(settingId);
        }
        NaireSettingQueryVo naireSettingQueryVo=new NaireSettingQueryVo();
        naireSettingQueryVo.setAreaTeamId(naireSettingVo.getAreaTeamId());
        naireSettingQueryVo.setPeriodId(naireSettingVo.getPeriodId());
        naireSettingQueryVo.setGradeIds(gradeIds);
        naireSettingQueryVo.setSubjectIds(subjectIds);
        naireSettingQueryVo.setCampusIds(campusIds);
        List<QuestionnaireSetting> checkQuestionnaireSettingList=this.findSettingListByQueryVo(naireSettingQueryVo);
        if (checkQuestionnaireSettingList!=null && checkQuestionnaireSettingList.size()>0){
            StringBuffer errorMsg=new StringBuffer();
            for (QuestionnaireSetting questionnaireSetting:checkQuestionnaireSettingList){
                errorMsg.append(questionnaireSetting.getCampusName()+questionnaireSetting.getGradeName()+questionnaireSetting.getSubjectName()+"已经存在，请勿重复设置!\n");
            }
            throw new BusException(errorMsg.toString());
        }
        List<BaseGrade> baseGradeList=baseGradeService.findByIds(gradeIds,"gradeId","gradeName");
        List<BaseSubject> baseSubjectList=baseSubjectService.findByIds(subjectIds,"subjectId","subjectName");
        List<Campus> campusList=campusService.findByIds(campusIds,"campusId","campusName");
        List<QuestionnaireSetting> questionnaireSettingList=new ArrayList<>(gradeIds.size()*subjectIds.size()*campusIds.size());

        for (BaseGrade baseGrade:baseGradeList){
            for (BaseSubject baseSubject:baseSubjectList){
                for (Campus campus:campusList){
                    QuestionnaireSetting questionnaireSetting=new QuestionnaireSetting();
                    questionnaireSetting.setGradeId(baseGrade.getGradeId());
                    questionnaireSetting.setGradeName(baseGrade.getGradeName());
                    questionnaireSetting.setSubjectId(baseSubject.getSubjectId());
                    questionnaireSetting.setSubjectName(baseSubject.getSubjectName());
                    questionnaireSetting.setCampusId(campus.getCampusId());
                    questionnaireSetting.setCampusName(campus.getCampusName());
                    questionnaireSetting.setAreaTeamId(areaTeam.getAreaTeamId());
                    questionnaireSetting.setAreeTeamName(areaTeam.getName());
                    questionnaireSetting.setPeriodId(period.getPeriodId());
                    questionnaireSetting.setPeriodName(period.getPeriodName());
                    questionnaireSetting.setSettingId(settingId);
                    questionnaireSetting.setUseQuestionnaireId(naireSettingVo.getUseQuestionnaireId());
                    questionnaireSetting.setUseQuestionnaireName(naireSettingVo.getUseQuestionnaireName());
                    questionnaireSetting.setCreateTime(TimeUtils.now());
                    questionnaireSetting.setUpdateTime(TimeUtils.now());
                    questionnaireSettingList.add(questionnaireSetting);
                }
            }
        }
        if (questionnaireSettingList.size()>0){
            this.insertBatch(questionnaireSettingList);
        }


    }

    @Override
    public void deleteBySettingId(Long settingId) {
        if (settingId!=null){
            Example<QuestionnaireSetting> example=QuestionnaireSetting.createExample();
            example.createCriteria().andEqualTo("settingId",settingId);
            this.deleteByExample(example);
        }
    }

    @Override
    public QuestionnaireSetting findByQuestionnaireId(Long questionnaireId) {
        if (questionnaireId==null){
            throw new NullPointerException();
        }
        Example<QuestionnaireSetting> example=QuestionnaireSetting.createExample();
        example.createCriteria().andEqualTo("useQuestionnaireId",questionnaireId);
        return this.findOneByExample(example);
    }

    @Override
    public List<QuestionnaireSetting> findSettingListByQueryVo(NaireSettingQueryVo naireSettingQueryVo, String... colunms) {
        Example<QuestionnaireSetting> example=QuestionnaireSetting.createExample();
        Example.Criteria criteria=example.createCriteria();
        CriteriaUtils.fillCondition(criteria,naireSettingQueryVo);
        if (colunms!=null){
            example.selectProperties(colunms);
        }
        return this.findListByExample(example);
    }

    @Override
    public QuestionnaireSetting findSettingByQueryVo(NaireSettingQueryVo naireSettingQueryVo) {
        Example<QuestionnaireSetting> example=QuestionnaireSetting.createExample();
        Example.Criteria criteria=example.createCriteria();
        CriteriaUtils.fillCondition(criteria,naireSettingQueryVo);
        return this.findOneByExample(example);
    }
}